<!DOCTYPE html>  
<html>
    <meta charset="utf-8" />  
    <title>单链表</title>  
<body>
    <h2>单链表</h2>  
    https://blog.csdn.net/zhuwq585/article/details/53130741
</body>
<script language="javascript"type="text/javascript">  

    function LinkedList() {
        var Node = function(element) {
            this.element = element;//存放节点内容
            this.next = null;//指针
        }

        var length = 0,
            head = null;

        //新增
        this.append = function(element) {
            
            var node = new Node(element), //传入值创建Node项
                current;
            
            if(head === null) { //如果为空链表
                head = node; //设置node为head（head为第一个节点的引用
            } else {
                current = head; //从表头开始
                while(current.next) {
                    //循环列表，找到最后一项（列表最后一个节点的下一个元素始终是null）
                    current = current.next;
                }
                
                //使当前最后一项的指针指向node
                current.next = node;
            }
            length++; //更新列表长度
            return head;
        };

        this.removeAt = function(position) {
            if(position > -1 && position < length) { //有效性检测
                var current = head, //用current来循环列表
                previous,
                index = 0;

                if(position === 0) {
                    head = current.next; //移除第一个元素，直接把head指向下一个元素
                } else {
                    while(index++ < position) { //循环列表找到满足条件的那个元素
                        previous = current; //
                        current = current.next; //把下一个变量覆给current
                    }
                    //跳过current，将当前要移除的元素的上一个与下一项直接连接起来。
                    previous.next = current.next;
                }
                length --;
                return head;
            } else {
                return null;
            }
        }                               

    }

    var list = new LinkedList();
    // list.append({name:'xw',sex:1});
    // list.append([1,2,3]);
    // list.append("5");
    // list.append(3);
    // var endList = list.append(true);
    // console.log('endList',endList)
    // var removeList = list.removeAt(0)
    // console.log('removeList',removeList)

    var obj=[{
        name:'xw',
        sex:1
    },{
        name:'zy',
        sex:2
    }]



    obj.forEach((item)=>{
        list.append(item)
    })
    
    const st = list.append();
    console.log(st)

</script>  
</html>